/**
 * API服务模块
 * 封装具体的API调用方法
 */

import { request } from './request.js'
import { API_ENDPOINTS } from './config.js'

/**
 * 用户相关API
 */
export const userApi = {
  // 用户登录
  login(data) {
    return request.post(API_ENDPOINTS.USER.LOGIN, data)
  },

  // 用户注册
  register(data) {
    return request.post(API_ENDPOINTS.USER.REGISTER, data)
  },

  // 获取用户信息
  getUserInfo() {
    return request.get(API_ENDPOINTS.USER.INFO)
  },

  // 获取用户详细信息
  getUserDetail() {
    return request.get(API_ENDPOINTS.USER.GET)
  },

  // 更新用户信息（头像、背景色等）
  updateUserInfo(data) {
    return request.put(API_ENDPOINTS.USER.UPDATE, data)
  },

  // 更新用户资料
  updateProfile(data) {
    return request.put(API_ENDPOINTS.USER.UPDATE_PROFILE, data)
  },

  // 修改登录密码
  changePassword(data) {
    return request.put(API_ENDPOINTS.USER.CHANGE_PASSWORD, data)
  },

  // 修改支付密码
  changePayPassword(data) {
    return request.put(API_ENDPOINTS.USER.CHANGE_PAY_PASSWORD, data)
  },

  // 创建支付密码
  createPayPassword(data) {
    return request.post(API_ENDPOINTS.USER.CREATE_PAY_PASSWORD, data)
  },

  // 更新用户语言设置
  updateLanguage(lang) {
    return request.post(`${API_ENDPOINTS.USER.UPDATE_LANG}?lang=${lang}`)
  },

  // 用户登出
  logout() {
    return request.post(API_ENDPOINTS.USER.LOGOUT)
  },

  // 更新用户邮箱
  updateEmail(data) {
    return request.put(API_ENDPOINTS.USER.UPDATE_EMAIL, data)
  }
}

/**
 * 认证相关API
 */
export const authApi = {
  // 发送邮箱验证码
  sendMailCode(data) {
    return request.post(API_ENDPOINTS.AUTH.SEND_MAIL_CODE, data)
  },

  // 获取用户认证状态
  getUserAuthStatus() {
    return request.get(API_ENDPOINTS.AUTH.USER_AUTH_STATUS)
  }
}

/**
 * 收益相关API
 */
export const incomeApi = {
  // 获取收益
  getIncome() {
    return request.get(API_ENDPOINTS.INCOME.GET)
  },

  // 获取利润
  getProfit() {
    return request.get(API_ENDPOINTS.INCOME.GET_PROFIT)
  },

  // 获取用户收益K线数据
  getKline() {
    return request.get('/app-api/option/order/list');
  },
  getStockPage() {
    return request.get('/app-api/option/stock/page');
  },
  // 新增：获取推荐产品板块接口
  getGoodsStockPage(params) {
    return request.get('/app-api/goods/stock/page', params);
  },
  // 新增：获取公告信息
  getJournalismContent(params) {
    return request.get('/app-api/journalism/content/page', params);
  },
  // 新增：获取滚动图信息
  getJournalismBanner(params) {
    return request.get('/app-api/journalism/banner/page', params);
  },
};;;

/**
 * 投资收益相关API
 */
export const investApi = {
  // 获取量化收益数据
  getProfit() {
    return request.get(API_ENDPOINTS.INVEST.GET_PROFIT)
  },

  // 获取产品分类列表
  getProductsCategory() {
    return request.get(API_ENDPOINTS.INVEST.PRODUCTS_CATEGORY_LIST)
  },

  // 获取投资品分页数据
  getProductsPage(params) {
    return request.get(API_ENDPOINTS.INVEST.PRODUCTS_PAGE, params)
  },

  // 获取产品详情
  getProductDetail(id) {
    return request.get(API_ENDPOINTS.INVEST.PRODUCTS_GET, { id })
  }
}

/**
 * 投资相关API
 */
export const investmentApi = {
  // 获取投资列表
  getList(params) {
    return request.get(API_ENDPOINTS.INVESTMENT.LIST, params)
  },

  // 获取投资详情
  getDetail(id) {
    return request.get(`${API_ENDPOINTS.INVESTMENT.DETAIL}/${id}`)
  },

  // 创建投资
  create(data) {
    return request.post(API_ENDPOINTS.INVESTMENT.CREATE, data)
  },

  // 更新投资
  update(id, data) {
    return request.put(`${API_ENDPOINTS.INVESTMENT.UPDATE}/${id}`, data)
  },

  // 删除投资
  delete(id) {
    return request.delete(`${API_ENDPOINTS.INVESTMENT.DELETE}/${id}`)
  }
}

/**
 * 行情相关API
 */
export const marketApi = {
  // 获取行情数据
  getQuotes(params) {
    return request.get(API_ENDPOINTS.MARKET.QUOTES, params)
  },

  // 获取历史数据
  getHistory(params) {
    return request.get(API_ENDPOINTS.MARKET.HISTORY, params)
  },

  // 获取实时数据
  getRealtime(params) {
    return request.get(API_ENDPOINTS.MARKET.REALTIME, params)
  }
}

/**
 * 持仓相关API
 */
export const positionApi = {
  // 获取持仓列表
  getList(params) {
    return request.get(API_ENDPOINTS.POSITION.LIST, params)
  },

  // 获取持仓详情
  getDetail(id) {
    return request.get(`${API_ENDPOINTS.POSITION.DETAIL}/${id}`)
  },

  // 买入
  buy(data) {
    return request.post(API_ENDPOINTS.POSITION.BUY, data)
  },

  // 卖出
  sell(data) {
    return request.post(API_ENDPOINTS.POSITION.SELL, data)
  }
}

/**
 * 量化相关API
 */
export const quantitativeApi = {
  // 获取策略列表
  getStrategies(params) {
    return request.get(API_ENDPOINTS.QUANTITATIVE.STRATEGIES, params)
  },

  // 回测
  backtest(data) {
    return request.post(API_ENDPOINTS.QUANTITATIVE.BACKTEST, data)
  },

  // 获取绩效数据
  getPerformance(params) {
    return request.get(API_ENDPOINTS.QUANTITATIVE.PERFORMANCE, params)
  }
}

/**
 * 质押相关API
 */
export const pledgeApi = {
  // 获取质押订单分页数据
  getOrderPage(params) {
    return request.get(API_ENDPOINTS.PLEDGE.ORDER_PAGE, params)
  },
  
  // 获取质押订单详情
  getOrderDetail(id) {
    return request.get(`${API_ENDPOINTS.PLEDGE.ORDER_DETAIL}?id=${id}`)
  }
}

/**
 * 充值相关API
 */
export const rechargeApi = {
  // 获取充值订单分页数据
  getOrderPage(params) {
    return request.get(API_ENDPOINTS.RECHARGE.ORDER_PAGE, params)
  }
}

/**
 * 提现相关API
 */
export const withdrawApi = {
  // 获取提现订单分页
  getOrderPage(params) {
    return request.get(API_ENDPOINTS.WITHDRAW.ORDER_PAGE, { params })
  },
  
  // 创建提现申请
  create(data) {
    return request.post(API_ENDPOINTS.WITHDRAW.CREATE, data)
  }
}

/**
 * 钱包地址相关API
 */
export const walletAddressApi = {
  // 获取钱包地址分页
  getPage(params) {
    return request.get(API_ENDPOINTS.WALLET_ADDRESS.PAGE, { params })
  },
  
  // 创建钱包地址
  create(data) {
    return request.post(API_ENDPOINTS.WALLET_ADDRESS.CREATE, data)
  },
  
  // 更新钱包地址
  update(id, data) {
    return request.put(`${API_ENDPOINTS.WALLET_ADDRESS.UPDATE}/${id}`, data)
  },
  
  // 删除钱包地址
  delete(id) {
    return request.delete(`${API_ENDPOINTS.WALLET_ADDRESS.DELETE}/${id}`)
  }
}

/**
 * 文件上传API
 */
export const uploadApi = {
  // 上传图片
  uploadImage(filePath, options = {}) {
    return request.upload(API_ENDPOINTS.UPLOAD.IMAGE, filePath, {
      name: 'image',
      ...options
    })
  },

  // 上传文件
  uploadFile(filePath, options = {}) {
    return request.upload(API_ENDPOINTS.UPLOAD.FILE, filePath, {
      name: 'file',
      ...options
    })
  }
}

/**
 * FAQ文章相关API
 */
export const faqApi = {
  // 获取FAQ文章列表（分页）
  getArticleList(params) {
    return request.get(API_ENDPOINTS.FAQ.LIST, params)
  },

  // 获取FAQ文章详情
  getArticleDetail(id) {
    return request.get(`${API_ENDPOINTS.FAQ.DETAIL}?id=${id}`)
  },

  // 搜索FAQ文章
  searchArticles(params) {
    return request.get(API_ENDPOINTS.FAQ.SEARCH, params)
  }
}

/**
 * 邀请相关API
 */
export const inviteApi = {
  // 获取邀请收益统计
  getProfitSummary() {
    return request.get(API_ENDPOINTS.INVITE.PROFIT_SUMMARY)
  },

  // 获取邀请记录分页列表
  getProfitPage(params) {
    return request.get(API_ENDPOINTS.INVITE.PROFIT_PAGE, { params })
  },

  // 获取邀请收益排行榜
  getTotalProfitRank(params) {
    return request.get(API_ENDPOINTS.INVITE.TOTAL_PROFIT_RANK, params)
  }
}

/**
 * 消息相关API
 */
export const messageApi = {
  // 获取我的消息分页列表
  getMyPage(params) {
    return request.get(API_ENDPOINTS.MESSAGE.MY_PAGE, { params })
  },
  
  // 更新消息已读状态
  updateRead(ids) {
    return request.put(API_ENDPOINTS.MESSAGE.UPDATE_READ, { ids })
  }
}

/**
 * TRON订单相关API
 */
export const tronOrderApi = {
  // 创建TRON订单
  create(data) {
    return request.post(API_ENDPOINTS.TRON_ORDER.CREATE, data)
  },
  
  // 根据订单ID获取订单详情
  getByOrder(orderId) {
    return request.get(API_ENDPOINTS.TRON_ORDER.GET_BY_ORDER, { orderId })
  }
}

/**
 * 商品相关API
 */
export const goodsApi = {
  // 获取产品分页列表
  getStockPage(params) {
    return request.get(API_ENDPOINTS.GOODS.STOCK_PAGE, params)
  },
  
  // 获取推荐热门产品
  getHotStocks(params = {}) {
    const defaultParams = {
      pageNo: 1,
      pageSize: 10,
      hotStatus: 0, // 0-是热门推荐
      tradeStatus: 0, // 0-开启交易
      status: 0 // 0-开启状态
    }
    return request.get(API_ENDPOINTS.GOODS.STOCK_PAGE, { ...defaultParams, ...params })
  },
  
  // 获取产品详情
  getStockDetail(id) {
    return request.get(`${API_ENDPOINTS.GOODS.STOCK_GET}?id=${id}`)
  },
  
  // 获取产品K线数据
  getStockKline(params) {
    return request.get(API_ENDPOINTS.GOODS.STOCK_KLINE, params)
  },
  
  // 创建收藏
  createStockFavorite(data) {
    return request.post(API_ENDPOINTS.GOODS.STOCK_FAVORITES_CREATE, data)
  },
  
  // 删除收藏
  deleteStockFavorite(id) {
    return request.delete(`${API_ENDPOINTS.GOODS.STOCK_FAVORITES_DELETE}?id=${id}`)
  },
  
  // 根据产品获取配置列表
  getStockConfigList(productId) {
    return request.get(`${API_ENDPOINTS.GOODS.STOCK_CONFIG_LIST}?productId=${productId}`)
  },
  
  // 获取产品K线数据
  getKlineData(params) {
    return request.get(API_ENDPOINTS.GOODS.STOCK_KLINE, params)
  },
  
  // 获取常用K线数据
  getCommonKline(params) {
    return request.get(API_ENDPOINTS.GOODS.STOCK_COMMON_KLINE, params)
  },
  
  // 获取自选产品分页列表
  getStockFavoritesPage(params) {
    return request.get('/app-api/goods/stock-favorites/page', params)
  }
}

/**
 * 期权订单相关API
 */
export const optionApi = {
  // 获取持仓订单分页数据
  getOrderPage(params) {
    return request.get(API_ENDPOINTS.OPTION.ORDER_PAGE, params)
  },
  
  // 获取订单详情
  getOrderDetail(id) {
    return request.get(API_ENDPOINTS.OPTION.ORDER_GET, { id })
  },
  
  // 提交下单
  submitOrder(data) {
    return request.post(API_ENDPOINTS.OPTION.ORDER_SUBMIT, data)
  },
  
  // 获取订单手续费
  getOrderFee(data) {
    return request.post(API_ENDPOINTS.OPTION.ORDER_FEE, data)
  }
}

// 默认导出所有API
export default {
  userApi,
  authApi,
  incomeApi,
  investApi,
  investmentApi,
  marketApi,
  positionApi,
  quantitativeApi,
  pledgeApi,
  rechargeApi,
  withdrawApi,
  walletAddressApi,
  uploadApi,
  faqApi,
  inviteApi,
  messageApi,
  tronOrderApi,
  goodsApi,
  optionApi
}